<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="acatar_box">
        <img src="" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        label-width="60px"
        class="login_form"
        ref="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="loginForm.username"
            class="input"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            class="input"
            type="password"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="danger" plain @click="resetLoginForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { reqLogin } from "../../api";
export default {
  data() {
    return {
      //登录表单的数据
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        userName: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6到 15个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 表单重置
    resetLoginForm() {
      this.$refs.loginForm.resetFields();
    },
    // 表单预验证，提交表单前进行前台验证，验证无误后再发请求到后端
    login() {
      this.$refs.loginForm.validate(async (valid, errorObj) => {
        //valid是布尔值，验证通过true，errorObj是没通过验证的字段
        console.log(valid, errorObj);
        if (!valid) return;
        else {
          // const result = await this.$http.post("login", this.loginForm);
          const result = await reqLogin(this.loginForm);

          console.log(result);
          // 提示登录成功与失败
          if (result.meta.status != 200) this.$message.error(result.meta.msg);
          else this.$message.success(result.meta.msg);

          //登录成功之后，1.将返回的token 保存到客户端 sessionStorage中 2.通过编程式导航跳转到/home页面
          const token = result.data.token;
          sessionStorage.setItem("token_key", token);
          this.$router.replace("/home");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    position: absolute;
    width: 500px;
    height: 350px;
    background-color: aliceblue;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -60%);
    .acatar_box {
      position: absolute;
      height: 100px;
      width: 100px;
      background-color: aqua;
      border-radius: 50%;
      // top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    .login_form {
      position: absolute;
      bottom: 0;
      right: 0;
      width: 100%;
      .input {
        width: 90%;
      }
      .btns {
        margin-top: 100px;
        margin-right: 50px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
